import axios from 'axios'
export default function ajax(url = '', params = {}, type = 'get') {
    var promise
    return new Promise((resolve, reject) => {
        if (type == 'get') {
            let paramsStr = ''
            Object.keys(params).forEach(key => {
                paramsStr += key + '=' + params[key] + '&'
            })
            if (paramsStr != '') {
                paramsStr = paramsStr.substr(0, paramsStr.lastIndexOf('&'))
            }
            url += '?' + paramsStr
            promise = axios.get(url)
        } else if (type == 'post') {
            promise = axios.post(url, params)
        }
        promise.then(response => {
            resolve(response.data)
        }).catch(err => {
            reject(err)
        })
    })
}


// 使用

//http://localhost:3000/userlist?page=1&size=10
// ajax(
//     url: "http://localhost:3000/userlist",
//     type: "get",
//     params: {page:1,size:10}
// )